package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.service.SystemLabelService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.BaseDTO;
import com.xbongbong.paas.toolbox.wrap.BaseVO;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.label.pojo.dto.LabelSortDTO;
import com.xbongbong.pro.label.pojo.vo.LabelGroupListVO;
import com.xbongbong.pro.label.pojo.vo.LabelSortVO;
import com.xbongbong.pro.systemlabel.pojo.dto.SystemLabelAddBatchDTO;
import com.xbongbong.pro.systemlabel.pojo.dto.SystemLabelAddDTO;
import com.xbongbong.pro.systemlabel.pojo.dto.SystemLabelCompleteDeleteDTO;
import com.xbongbong.pro.systemlabel.pojo.dto.SystemLabelDeleteBatchDTO;
import com.xbongbong.pro.systemlabel.pojo.dto.SystemLabelDeleteDTO;
import com.xbongbong.pro.systemlabel.pojo.dto.SystemLabelListDTO;
import com.xbongbong.pro.systemlabel.pojo.dto.SystemLabelRecoverDTO;
import com.xbongbong.pro.systemlabel.pojo.dto.SystemLabelUpdateDTO;
import com.xbongbong.pro.systemlabel.pojo.vo.SystemLabelAddBatchVO;
import com.xbongbong.pro.systemlabel.pojo.vo.SystemLabelAddVO;
import com.xbongbong.pro.systemlabel.pojo.vo.SystemLabelCompleteDeleteVO;
import com.xbongbong.pro.systemlabel.pojo.vo.SystemLabelDeleteBatchVO;
import com.xbongbong.pro.systemlabel.pojo.vo.SystemLabelDeleteVO;
import com.xbongbong.pro.systemlabel.pojo.vo.SystemLabelListVO;
import com.xbongbong.pro.systemlabel.pojo.vo.SystemLabelPersonAddDTO;
import com.xbongbong.pro.systemlabel.pojo.vo.SystemLabelRecoverVO;
import com.xbongbong.pro.systemlabel.pojo.vo.SystemLabelUpdateVO;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * 系统标签相关接口
 * @author 魏荣杰
 *
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/sysLabel")
public class SystemLabelController {

    @Resource
    private SystemLabelService systemLabelService;


    /**
     * Description: 系统标签新建
     * @param systemLabelAddDTO
	 * @param br
     * @return java.lang.String
     * @throws
     * @author 魏荣杰
     * @date 2021/12/2 17:23
     * @since
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String add(@RequestBody @Valid SystemLabelAddDTO systemLabelAddDTO, BindingResult br) throws Exception {
        XbbResponse<SystemLabelAddVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                SystemLabelAddVO systemLabelAddVO = systemLabelService.add(systemLabelAddDTO);
                response = new XbbResponse<>(systemLabelAddVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }

        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }


    /**
     * Description: 系统标签编辑
     * @param systemLabelUpdateDTO
     * @param br
     * @return java.lang.String
     * @throws
     * @author 魏荣杰
     * @date 2021/12/2 17:23
     * @since
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String update(@RequestBody @Valid SystemLabelUpdateDTO systemLabelUpdateDTO, BindingResult br) throws Exception {
        XbbResponse<SystemLabelUpdateVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                SystemLabelUpdateVO systemLabelUpdateVO = systemLabelService.update(systemLabelUpdateDTO);
                response = new XbbResponse<>(systemLabelUpdateVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }

        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }



    /**
     * Description: 系统标签删除
     * @param systemLabelDeleteDTO
	 * @param br
     * @return java.lang.String
     * @throws 
     * @author 魏荣杰
     * @date 2021/12/15 19:22
     * @since
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String delete(@RequestBody @Valid SystemLabelDeleteDTO systemLabelDeleteDTO, BindingResult br) throws Exception {
        XbbResponse<SystemLabelDeleteVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                SystemLabelDeleteVO systemLabelDeleteVO = systemLabelService.delete(systemLabelDeleteDTO);
                response = new XbbResponse<>(systemLabelDeleteVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }

        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * Description: 系统标签批量添加
     * @param systemLabelAddBatchDTO
	 * @param br
     * @return java.lang.String
     * @throws
     * @author 魏荣杰
     * @date 2021/12/15 19:22
     * @since
     */
    @RequestMapping(value = "/addBatch", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String addBatch(@RequestBody @Valid SystemLabelAddBatchDTO systemLabelAddBatchDTO, BindingResult br) throws Exception {
        XbbResponse<SystemLabelAddBatchVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                SystemLabelAddBatchVO systemLabelAddBatchVO = systemLabelService.addBatch(systemLabelAddBatchDTO);
                response = new XbbResponse<>(systemLabelAddBatchVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }

        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }


    /**
     * Description: 系统标签批量删除
     * @param systemLabelDeleteBatchDTO
	 * @param br
     * @return java.lang.String
     * @throws
     * @author 魏荣杰
     * @date 2021/12/15 19:25
     * @since
     */
    @RequestMapping(value = "/deleteBatch", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String deleteBatch(@RequestBody @Valid SystemLabelDeleteBatchDTO systemLabelDeleteBatchDTO, BindingResult br) throws Exception {
        XbbResponse<SystemLabelDeleteBatchVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                SystemLabelDeleteBatchVO systemLabelDeleteBatchVO = systemLabelService.deleteBatch(systemLabelDeleteBatchDTO);
                response = new XbbResponse<>(systemLabelDeleteBatchVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }

        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }


    /**
     * Description: 系统标签列表
     * @param systemLabelListDTO
	 * @param br
     * @return java.lang.String
     * @throws
     * @author 魏荣杰
     * @date 2021/12/15 19:25
     * @since
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String list(@RequestBody @Valid SystemLabelListDTO systemLabelListDTO, BindingResult br) throws Exception {
        XbbResponse<SystemLabelListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                SystemLabelListVO systemLabelListVO = systemLabelService.list(systemLabelListDTO);
                response = new XbbResponse<>(systemLabelListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }

        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }


    /**
     * Description: 系统标签恢复
     * @param systemLabelRecoverDTO
	 * @param br
     * @return java.lang.String
     * @throws
     * @author 魏荣杰
     * @date 2021/12/15 19:26
     * @since
     */
    @RequestMapping(value = "/recover", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String recover(@RequestBody @Valid SystemLabelRecoverDTO systemLabelRecoverDTO, BindingResult br) throws Exception {
        XbbResponse<SystemLabelRecoverVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                SystemLabelRecoverVO systemLabelRecoverVO = systemLabelService.recover(systemLabelRecoverDTO);
                response = new XbbResponse<>(systemLabelRecoverVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }

        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }


    /**
     * Description: 系统标签彻底删除
     * @param systemLabelCompleteDeleteDTO
	 * @param br
     * @return java.lang.String
     * @throws
     * @author 魏荣杰
     * @date 2021/12/15 19:26
     * @since
     */
    @RequestMapping(value = "/completeDelete", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String completeDelete(@RequestBody @Valid SystemLabelCompleteDeleteDTO systemLabelCompleteDeleteDTO, BindingResult br) throws Exception {
        XbbResponse<SystemLabelCompleteDeleteVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                SystemLabelCompleteDeleteVO systemLabelCompleteDeleteVO = systemLabelService.completeDelete(systemLabelCompleteDeleteDTO);
                response = new XbbResponse<>(systemLabelCompleteDeleteVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }

        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * Description: 系统标签排序
     * @param labelSortDTO
	 * @param br
     * @return java.lang.String
     * @throws
     * @author 魏荣杰
     * @date 2021/12/15 19:26
     * @since
     */
    @RequestMapping(value = "/sort", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String sort(@RequestBody @Valid LabelSortDTO labelSortDTO, BindingResult br) throws Exception {
        XbbResponse<LabelSortVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                LabelSortVO labelSortVO = systemLabelService.sort(labelSortDTO);
                response = new XbbResponse<>(labelSortVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }

        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }


    /**
     * Description: 系统标签分组列表
     * @param baseDTO
	 * @param br
     * @return java.lang.String
     * @throws
     * @author 魏荣杰
     * @date 2021/12/15 19:27
     * @since
     */
    @RequestMapping(value = "/groupList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String groupList(@RequestBody @Valid BaseDTO baseDTO, BindingResult br) throws Exception {
        XbbResponse<LabelGroupListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                LabelGroupListVO labelGroupListVO = systemLabelService.groupList(baseDTO);
                response = new XbbResponse<>(labelGroupListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }

        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }


    /**
     * Description: 系统标签列表（组件选择用）
     * @param baseDTO
	 * @param br
     * @return java.lang.String
     * @throws
     * @author 魏荣杰
     * @date 2021/12/15 19:27
     * @since
     */
    @RequestMapping(value = "/sampleList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String sampleList(@RequestBody @Valid BaseDTO baseDTO, BindingResult br) throws Exception {
        XbbResponse<SystemLabelListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                SystemLabelListVO systemLabelListVO = systemLabelService.sampleList(baseDTO);
                response = new XbbResponse<>(systemLabelListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }

        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }


    /**
     * Description: 人员添加系统标签
     * @param systemLabelPersonAddDTO
	 * @param br
     * @return java.lang.String
     * @throws
     * @author 魏荣杰
     * @date 2021/12/15 20:00
     * @since
     */
    @RequestMapping(value = "/personAdd", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String personAdd(@RequestBody @Valid SystemLabelPersonAddDTO systemLabelPersonAddDTO, BindingResult br) throws Exception {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BaseVO baseVO = systemLabelService.personAdd(systemLabelPersonAddDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }

        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }


}
